package com.manager.plat.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.manager.plat.dao.IConfigLowerPartnerDao;
import com.manager.plat.util.BaseUtil;
import com.manager.plat.vo.ConfigLowerPartnerVo;

public class ConfigLowerPartnerDaoImpl extends JdbcDaoSupport implements IConfigLowerPartnerDao {
	private static Logger logger = Logger.getLogger(ConfigLowerPartnerDaoImpl.class);
	
	/**查询所有渠道信息列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<ConfigLowerPartnerVo> getConfigLowerPartnerList() {
		String sql = "select clp.id, clp.name, clp.detail, clp.user_id, ui.login_username user_name " +
					 "from tb_ivr_config_lower_partner clp " +
					 "inner join tb_ivr_user_info ui on ui.id = clp.user_id and ui.status = 1 " +
					 "where clp.status = 1 " +
					 "order by clp.id desc";
		logger.info(String.format("查询所有渠道信息列表[%s]", sql));
		List<ConfigLowerPartnerVo> configLowerPartnerVoList = this.getJdbcTemplate().query(sql, rowMapper);
		if(configLowerPartnerVoList != null && configLowerPartnerVoList.size() > 0){
			return configLowerPartnerVoList;
		}
		
		return null;
	}
	/**根据Id获得渠道信息*/
	@SuppressWarnings("unchecked")
	@Override
	public ConfigLowerPartnerVo getConfigLowerPartnerById(int id) {
		String sql = "select clp.id, clp.name, clp.detail, clp.user_id, ui.login_username user_name " +
				 "from tb_ivr_config_lower_partner clp " +
				 "inner join tb_ivr_user_info ui on ui.id = clp.user_id and ui.status = 1 " +
				 "where clp.id = ? and clp.status = 1 " +
				 "order by clp.id desc";
		
		Object[] param = new Object[]{id};
		logger.info(String.format("根据Id获得渠道信息[%s]", BaseUtil.logSQL(sql, param)));
		
		List<ConfigLowerPartnerVo> configLowerPartnerVoList = this.getJdbcTemplate().query(sql, param, rowMapper);
		if(configLowerPartnerVoList != null && configLowerPartnerVoList.size() > 0){
			return configLowerPartnerVoList.get(0);
		}
		
		return null;
	}
	/**数据转换*/
	private ParameterizedRowMapper<ConfigLowerPartnerVo> rowMapper = new ParameterizedRowMapper<ConfigLowerPartnerVo>(){
		@Override
		public ConfigLowerPartnerVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			ConfigLowerPartnerVo configLowerPartnerVo = new ConfigLowerPartnerVo();
			configLowerPartnerVo.setId(rs.getInt("id"));
			configLowerPartnerVo.setName(rs.getString("name"));
			configLowerPartnerVo.setDetail(rs.getString("detail"));
			configLowerPartnerVo.setUser_id(rs.getString("user_id"));
			configLowerPartnerVo.setUser_name(rs.getString("user_name"));
			return configLowerPartnerVo;
		}
	};
	/**保存渠道信息*/
	@Override
	public int insertConfigLowerPartner(ConfigLowerPartnerVo configLowerPartnerVo) {
		String sql = "insert into tb_ivr_config_lower_partner(name,detail,user_id,sync_type,partner_type,status,add_user) values(?,?,?,0,2,1,?)";
		Object[] param = new Object[]{configLowerPartnerVo.getName(),configLowerPartnerVo.getDetail(),
									configLowerPartnerVo.getUser_id(),configLowerPartnerVo.getAdd_user()};
		logger.info(String.format("保存渠道信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**更新渠道信息*/
	@Override
	public int updateConfigLowerPartner(ConfigLowerPartnerVo configLowerPartnerVo) {
		String sql = "update tb_ivr_config_lower_partner set name = ?, detail = ?, user_id = ?, update_date = now(), update_user = ? where id = ?";
		Object[] param = new Object[]{configLowerPartnerVo.getName(),configLowerPartnerVo.getDetail(),
									configLowerPartnerVo.getUser_id(),configLowerPartnerVo.getUpdate_user(),
									configLowerPartnerVo.getId()};
		logger.info(String.format("更新渠道信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		
		return count;
	}
	/**删除渠道信息*/
	@Override
	public int deleteConfigLowerPartner(int id,int update_user) {
		String sql = "update tb_ivr_config_lower_partner set status = 0, update_date = now(), update_user = ? where id = ?";
		Object[] param = new Object[]{update_user, id};
		logger.info(String.format("删除渠道信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**根据渠道ID获得该渠道下所有的长号码*/
	@SuppressWarnings("unchecked")
	@Override
	public List<String> getLowerPartnerSpNumberById(int lower_id) {
		String sql = "select cr.sp_number " +
					 "from tb_ivr_config_lower_partner clp " +
					 "inner join tb_ivr_config_router cr on cr.lower_id = clp.id and cr.status = 1 " +
					 "where cr.lower_id = ? and  clp.status = 1";
		Object[] param = new Object[]{lower_id};
		logger.info(String.format("根据渠道ID获得该渠道下所有的长号码[%s]", BaseUtil.logSQL(sql, param)));
		
		List<String> sp_numberList = this.getJdbcTemplate().query(sql, param, tempMapper);
		if(sp_numberList != null && sp_numberList.size() > 0){
			return sp_numberList;
		}
		
		return null;
	}
	
	/**数据转换*/
	private ParameterizedRowMapper<String> tempMapper = new ParameterizedRowMapper<String>(){
		@Override
		public String mapRow(ResultSet rs, int rowNum) throws SQLException {
			String sp_number = rs.getString("sp_number");
			return sp_number;
		}
	};

	@SuppressWarnings("unchecked")
	@Override
	public int getLowerIdByUserId(int user_id) {
		String sql = "select id from tb_ivr_config_lower_partner clp where user_id = ?";
		Object[] param = new Object[]{user_id};
		logger.info(String.format("根据用户ID获得渠道ID[%s]", BaseUtil.logSQL(sql, param)));
		List<Integer> lowerIdList = this.getJdbcTemplate().query(sql, param, idMapper);
		if(lowerIdList != null && lowerIdList.size() > 0){
			return lowerIdList.get(0);
		}
		return 0;
	}
	/**数据转换*/
	private ParameterizedRowMapper<Integer> idMapper = new ParameterizedRowMapper<Integer>(){
		@Override
		public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
			int lower_id = rs.getInt("id");
			return lower_id;
		}
	};
}